package com.yundingai.controller;

import com.yundingai.ai.cache.RedisChatMemory;
import com.yundingai.domain.entity.RoomChat;
import com.yundingai.domain.vo.ChatVO;
import com.yundingai.domain.vo.RoomChatVo;
import com.yundingai.service.ChatService;
import com.yundingai.service.RoomChatService;
import com.yundingai.vm.ResponseResult;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import reactor.core.publisher.Flux;

import java.io.IOException;
import java.util.List;

/**
 * @Author: liyuxiang
 * @Date: 2025/7/17 13:57
 */

@RestController
public class ChatController {
    @Autowired
    ChatService chatService;
    @Autowired
    RoomChatService roomChatService;
    /**
     * 处理流式聊天请求，返回服务器发送事件（SSE）格式的响应流
     *
     * @param chatVO 用户输入的聊天问题
     * @return 包含逐条聊天响应的响应式数据流，通过Server-Sent Events协议传输
     */
    @PostMapping(value = "stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<ChatResponse> chatStream(@RequestBody ChatVO chatVO) {
        return chatService.chatStream(chatVO);
    }
    @PostMapping(value = "streamMessage", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<ChatResponse> streamMessage(@RequestBody ChatVO request,HttpServletRequest requests) {

       return chatService.streamMessage(request);
    }
    @GetMapping("/list")
    public ResponseResult list(@RequestParam String sessionId) {
        return ResponseResult.okResult(chatService.getChatMessages(sessionId));
    }
    @DeleteMapping("/clear/{sessionId}")
    public ResponseResult remove(@PathVariable String sessionId) {
        chatService.removeChatMessages(sessionId);
        return ResponseResult.okResult();
    }
    @DeleteMapping("/remove/{sessionId}/{messageId}")
    public ResponseResult removeByMessageId(@PathVariable String sessionId, @PathVariable String messageId) {
        chatService.removeByMessageId(sessionId, messageId);
        return ResponseResult.okResult();
    }

    @PostMapping(value = "testStream")
    public SseEmitter testStream(@RequestBody ChatVO chatVO) {
        SseEmitter sseEmitter = new SseEmitter();
        Flux<String> content = chatService.testStream(chatVO.getQuestion(), chatVO.getSessionId());

        try {
            sseEmitter.send(content);
            System.out.println(sseEmitter.toString());

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return sseEmitter;
    }
    @GetMapping("/roomChat/{roomId}")
    public ResponseResult roomChatList(@PathVariable String roomId) {
        List<RoomChatVo> roomChatList = roomChatService.getRoomChatList(roomId);
        return ResponseResult.okResult(roomChatList);
    }
    @PostMapping("/sendRoomChatMessage")
    public ResponseResult sendRoomChat(@RequestBody RoomChatVo roomChatVo) {
        roomChatService.send(roomChatVo);
        return ResponseResult.okResult();
    }


//    public SseEmitter sseChat(@RequestBody ChatVO chatVO, HttpServletRequest request) {
//        return sseService.sseChat(chatRequest,request);
//    }


}
